package com.uu.plugin;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;

import java.util.Collection;
import java.util.Objects;

@UtilityClass
public class ObjectUtil {

    /**
     * 获得对象的属性值
     *
     * @param src          原始对象
     * @param propertyName 属性名称
     * @return 属性值
     */
    @SneakyThrows
    public static Object getValue(Object src, String propertyName) {
        MetaObject metaObject = MetaObject.forObject(src, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
        return metaObject.getValue(propertyName);
    }

    /**
     * 设置对象的属性值
     *
     * @param src          源对象
     * @param propertyName 属性名称
     * @param value        属性值
     */
    public static void setValue(Object src, String propertyName, Object value) {
        MetaObject metaObject = MetaObject.forObject(src, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
        metaObject.setValue(propertyName, value);
    }

    public static boolean isEmpty(Collection collection) {
        return Objects.isNull(collection) || collection.isEmpty();
    }
}
